<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 商品审核列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <!-- 表格数据 start -->
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="user.account" label="用户"></el-table-column>
                <el-table-column prop="goodsCategory.name" label="类别"></el-table-column>
                <el-table-column prop="voice" label="语音地址"></el-table-column>
                <el-table-column prop="description" label="描述"></el-table-column>
                <el-table-column prop="money" label="价格(元)"></el-table-column>
                <el-table-column prop="authGoods" label="商品状态">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.authGoods === 'y'" type="success">审核通过</el-tag>
                        <el-tag v-else-if="scope.row.authGoods === 'n'" type="danger">未审核</el-tag>
                        <el-tag v-else type="warning">待审核</el-tag>
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <!-- <el-button type="text" icon="el-icon-lx-attention" @click="handleView(scope.$index, scope.row)"> 查看 </el-button> -->
                        <el-button type="text" icon="el-icon-lx-roundcheck" class="green" @click="handleAgree(scope.$index, scope.row)"
                            >通过</el-button
                        >
                        <el-button type="text" icon="el-icon-lx-roundclose" class="red" @click="handleRefuse(scope.$index, scope.row)"
                            >驳回</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <!-- 表格数据 end -->

            <!-- 分页区 start -->
            <div class="block">
                <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page.sync="pageNum"
                    :page-size="3"
                    layout="prev, pager, next, jumper"
                    :page-count="totalPages"
                >
                </el-pagination>
            </div>
            <!-- 分页区 end -->
        </div>
    </div>
</template>

<script>
export default {
    name: 'basetable',
    data() {
        return {
            goodsVo: {
                goods: {
                    userId: '',
                    categoryId: '',
                    voice: '',
                    description: '',
                    money: '',
                    saleNum: '',
                    authGoods: 'c',
                    user: {
                        account: ''
                    },
                    goodsCategory: {
                        name: ''
                    }
                },
                pageNum: 1
            },
            pageNum: 1,
            totalPages: 0,
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            form: {},
            idx: -1,
            id: -1
        };
    },

    created() {
        this.getData();
    },

    methods: {
        // 获取 goodsList 数据
        getData() {
            this.$axios.post('/admin/goods/list', this.goodsVo).then((r) => {
                this.tableData = r.data.data.data;
                console.log(r);
                this.totalPages = r.data.data.totalPages;
                console.log(this.totalPages);
            });
        },
        // 查看详情
        handleView(index, row) {},
        // 通过审核
        handleAgree(index, row) {
            this.$axios.get('/admin/goods/authPw/agree', { params: { id: row.id } }).then((r) => {
                if (r.data.code == 200) {
                    this.$message.success('通过成功');
                    this.tableData.splice(index, 1);
                } else {
                    this.$message.warning('操作失败，请稍候再试');
                }
            });
        },
        // 拒绝审核
        handleRefuse(index, row) {
            // 二次确认删除
            this.$confirm('确定要驳回该用户的商品审核吗？', '提示', {
                type: 'warning'
            }).then(() => {
                this.$axios.get('/admin/goods/authPw/refuse', { params: { id: row.id } }).then((r) => {
                    if (r.data.code == 200) {
                        this.$message.success('操作成功');
                        this.tableData.splice(index, 1);
                    } else {
                        this.$message.warning('操作失败，请稍候再试');
                    }
                });
            });
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },

        // 分页导航
        handleCurrentChange(pageNum) {
            this.goodsVo.pageNum = pageNum;
            this.getData();
            console.log(`当前页: ${pageNum}`);
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.green {
    color: #06710c;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
